﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using WPFExcelReport.Helpers;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YGRCPGB04 : DocumentBase
    {
        #region-------------Properties-----------------------
        #region ==BoxNum===========
        public static readonly DependencyProperty BoxNumProperty = DependencyProperty.Register("BoxNum",
            typeof(string), typeof(YGRCPGB04),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnBoxNumChanged));
        public string BoxNum
        {
            get
            {
                return (string)GetValue(BoxNumProperty);
            }
            set
            {
                SetValue(BoxNumProperty, value);
            }
        }
        private static void OnBoxNumChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as YGRCPGB04;
            if (src != null)
            {
                src.BoxNum = (string)e.NewValue;
            }
        }
        #endregion
        string sql = "";
        DataTable TResult = null;
        
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        string ABFAGANG = "";
        string ROWID = "";
        bool IsLoaded = false;
        DataRow CurrentRow = null;
        DataSet SResult = null;
        DataRow LastRow = null;
        DataSet SetMap = null;
        public DataSet dsResult;
        MainViewModel MainViewModel;

        public SETINTEXDB SetData
        {
            get
            {
                return MainViewModel.SetData;
            }
        }
        public SETINTEXDB SetEnv
        {
            get
            {
                return MainViewModel.SetEnv;
            }
        }

        public SETINTEXDB SetProd
        {
            get
            {
                return MainViewModel.SetProd;
            }
        }
        CollectionViewSource vwTPDC
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTPDC");
            }
        }

        SQLiteDatabase SQLiteDatabase
        {
            get
            {
                return MainViewModel.SQLiteDatabase;
            }
        }
        DataTable TBResult;
        VGLLG VGLLG = new VGLLG();
        #endregion
        public YGRCPGB04()
        {
            InitializeComponent();
            this.Name = "成品入库";
            if (INTEXDBC == null)
            {
                app.Load();
            }
            MainViewModel = new MainViewModel();
            DataContext = MainViewModel;
            EventDispatcher.Instance.OnStatusChange += Instance_OnStatusChange;
            txtMessage.Visibility = Visibility.Collapsed;
        }
        void Instance_OnStatusChange(object sender, StatusEventArgs e)
        {
            MessageInfo msg = ((StatusEventArgs)e).Info;
            string Message = msg.Details.Trim();
            if (Message != "")
            {
                txtMessage.Text = msg.Details;
                if (Message.Contains("YGRCPGB04"))
                {
                    txtMessage.Visibility = Visibility.Visible;
                }
                if (Message.Contains("YGRCPGB04启动"))
                {
                    txtMessage.Visibility = Visibility.Visible;
                }
                if (Message.Contains("YGRCPGB04完成"))
                {
                    txtMessage.Visibility = Visibility.Collapsed;
                }
                if (Message.Contains("YGRCPGB04异常终止"))
                {
                    txtMessage.Visibility = Visibility.Collapsed;
                }

            }
        }
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = Helpers.Helper.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(SPassword);
            string Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
            DataRow dwHR = app.UserValid(userID, Password);
            if (dwHR == null)
            {
                MessageBox.Show("请检查登陆信息");
                return;
            }
            txtLogIn.Background = new SolidColorBrush(Colors.Green);
            expLog.IsExpanded = false;
            expLog.Visibility = Visibility.Collapsed;
            gdMain.Visibility = Visibility.Visible;
        }
        /// <summary>
        /// SetProd 当前订单
        /// SetData保存所有订单，但是用于展示今日订单
        /// SetEnv保存所有订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true;
                if (app.User.Trim() == "ITXV")
                {
                    txtUserID.Text = app.User;
                    txtPassword.Password = app.Pwd;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnConvert_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataRow dw = SetData.Tables["GLLG"].Rows[0];
                DataSet SR = new DataSet();
                string bem = "",part = "";
                SR = VGLLG.QueryGLLG(dw, 7);
                if (SR.Tables.Count > 0)
                {
                    foreach (DataRow drow in SR.Tables["GLLG"].Rows)
                    {
                        bem = Helper.Convert8859P1ToGB2312(drow["JP10"].ToString());
                        part = drow["GPFBPART"].ToString();
                    }
                }
                
                    vwXSGB11.txtGLBEM.Text = bem;
                    vwXSGB11.txtFBPART.Text = part;
                
                List<string> ListArtcile = new List<string>();
                SetData.ICC_GB1109.Clear();
                SetData.ICC_GB1109.Merge(SetData.Tables["GLLG"], false, MissingSchemaAction.Ignore);
                string _date = "", _time = "", _stime = "";
                string Article = "";
                
                Helper.GetSTime(ref _date, ref _time, ref _stime);
                string DD = INTEXDBC.GetDBDATE();
                string TT = INTEXDBC.GetDBTIME();
                if (DD == "0") DD = _date;
                if (TT == "0") TT = _time;
                foreach (DataRow row in SetData.ICC_GB1109.Rows)
                {
                    row["PROCESSED"] = "N";
                    row["JP01"] = "N";
                    row["JP02"] = "N";
                    row["JP03"] = "N";
                    if (row["JP04"].ToString().TrimEnd().Length < 10)
                    {
                        row["JP04"] = "";
                        row["JP05"] = Helper.ConvertGB2312To8859P1("自由");
                    }
                    row["JP06"] = "N";
                    row["JP07"] = "N";
                    row["JP09"] = "N";
                    row["JP09"] = "N";
                    //row["JP10"] = "N";
                    row["TARGETQTY"] = row["GLBSTKG"];
                    row["CHGDATE"] = DD;
                    row["CHGTIME"] = TT;
                    row["CHGUSER"] = app.User;

                    Article = row["GPGART"].ToString() + row["GPGFARBE"].ToString();
                    if (!ListArtcile.Contains(Article))
                    {
                        ListArtcile.Add(Article);
                    }
                }
                DataRow dwData = SetData.VKPOS.NewRow();
                SetData.VKPOS.Clear(); 
                foreach (string art in ListArtcile)
                {
                    dwData["VKPART"] = Helper.Left(art.Substring(0, 20).PadRight(24), 24);
                    dwData["VKPFARBE"] = Helper.Left(art.Substring(20).PadRight(10), 10);
                    SResult = INTEXDBC.QueryVKPOS(dwData, 10);
                    SetData.Merge(SResult); 
                } 
                SetData.GLLG.Clear();
                BoxNum = (SetData.ICC_GB1109.Rows.Count).ToString()+",箱";                
            }
              
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnEntryCP_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetProd.ICC_GB1109.Clear();
                string _date = "", _time = "", _stime = "";
                Helper.GetSTime(ref _date, ref _time, ref _stime);
                string DD = INTEXDBC.GetDBDATE();
                string TT = INTEXDBC.GetDBTIME();
                if (DD == "0") DD = _date;
                if (TT == "0") TT = _time;
                foreach (DataRow row in SetData.ICC_GB1109.Rows)
                {
                    string value=row["TARGETQTY"].ToString().Trim();
                    if (value == "0" || value == "")
                    {
                       
                        row["JP01"] = "Y";
                    }
                    row["NEWDATE"] = DD;
                    row["NEWTIME"] = TT;
                    row["NEWUSER"] = app.User;
                }
                SetProd.ICC_GB1109.Merge(SetData.Tables["ICC_GB1109"], false, MissingSchemaAction.Ignore);
                SetData.ICC_GB1109.Clear();
               
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

    }
}
